﻿Public Class Sales

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        ClearList()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Sales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CustomerDataSet.Customer' table. You can move, or remove it, as needed.
        SalesToolStripMenuItem.Enabled = False

        Dim databaseProvider As DatabaseProvider = databaseProvider.Instance()
        ComboBox1.DataSource = databaseProvider.GetAllEmployees()
        ComboBox1.DisplayMember = "fullName"
        ComboBox1.ValueMember = "Employee_ID"
        Dim val As Integer = ComboBox1.SelectedValue

    End Sub

    Private Sub BtnAdd_Click(sender As System.Object, e As System.EventArgs) Handles BtnAdd.Click
        NewCustomer.Show()
    End Sub

    Private Sub BtnLoad_Click(sender As System.Object, e As System.EventArgs) Handles BtnLoad.Click
        LoadDetails.Show()
    End Sub

    Private Sub ChooseLanguageToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ChooseLanguageToolStripMenuItem.Click
        Language.Show()
        Me.Close()
    End Sub

    Private Sub AdminToolsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AdminToolsToolStripMenuItem.Click
        Start.Show()
        Me.Close()
    End Sub

    Private Sub ProductsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ProductsToolStripMenuItem.Click
        Product.Show()
        Me.Close()
    End Sub

    Private Sub StaffMembersToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles StaffMembersToolStripMenuItem.Click
        Employee.Show()
        Me.Close()
    End Sub

    Private Sub CustomersToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CustomersToolStripMenuItem.Click
        Customer.Show()
        Me.Close()
    End Sub

    Private Sub OrderToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OrderToolStripMenuItem.Click
        Orders.Show()
        Me.Close()
    End Sub

    Sub ClearList()
        TxtCustomerID.Clear()
        TxtCustomerName.Clear()
        TxtCustomerSurname.Clear()
        TxtCustomerTown.Clear()
        TxtCustomerPostcode.Clear()
        TxtCustomerAddress1.Clear()
        TxtCustomerAddress2.Clear()
        TxtCustomerHome.Clear()
        TxtCustomerMobile.Clear()
    End Sub

    Private Sub BtnSearch_Click(sender As System.Object, e As System.EventArgs) Handles BtnSearch.Click
        Product.Show()
    End Sub

    Private Sub BtnAddP_Click(sender As System.Object, e As System.EventArgs) Handles BtnAddP.Click
        If lOrderId.Text = "none" Then
            MsgBox("You need to create new order first")
        Else

            Dim productId = Integer.Parse(TxtPID.Text)
            Dim qty = Integer.Parse(TxtQuantity.Text)
            Dim orderId = Integer.Parse(lOrderId.Text)

            Dim databaseProvider As DatabaseProvider = databaseProvider.Instance()
            Dim productInfo = databaseProvider.InsertOrderLine(productId, orderId, qty)

            Dim row1 As String() = New String() {productInfo("ProductId"), productInfo("ProductName"), productInfo("ProductPrice"), qty}
            SalesGrid.Rows.Add(row1)

            TxtTotal.Text = CountTotal(qty).ToString()
        End If
    End Sub

    Private Function CountTotal(qty As Integer) As Double
        Dim total As Double = 0
        Dim tmp As Double
        For Each row As DataGridViewRow In SalesGrid.Rows
            Double.TryParse(row.Cells("PPrice").Value, tmp)
            total = total + (qty * tmp)
        Next

        Return total
    End Function

    Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
        For Each dr As DataGridViewRow In SalesGrid.SelectedRows ' delete selected records
            If (SalesGrid.Rows.Count > 1) Then
                SalesGrid.Rows.Remove(dr)
            End If
        Next

        'TxtTotal.Text = CountTotal().ToString()
    End Sub

    Private Sub BtnCreateOrder_Click(sender As Object, e As EventArgs) Handles BtnCreateOrder.Click
        If TxtCustomerName.Text.Length < 1 Or ComboBox1.Text.Length < 1 Then
            MsgBox("Customer details and sales person required")
        Else
            Dim databaseProvider As DatabaseProvider = databaseProvider.Instance()
            Dim orderId = databaseProvider.InsertOrderAndGetId(Integer.Parse(TxtCustomerID.Text), Integer.Parse(ComboBox1.SelectedValue))
            lOrderId.Text = orderId
        End If

    End Sub

    Private Sub BtnSubmit_Click(sender As Object, e As EventArgs) Handles BtnSubmit.Click
        Orders.Show()
        Me.Close()
    End Sub
End Class